function ResInfo() {
    this.path = "";     //资源路径
    this.type = null;   //资源类型
    this.uuids = {};    //使用的资源id
    this.obj = null;    //资源对象
    this.ref = 0;       //引用计数
}

window.ResMgr = {
    resPool: {},
    uuidTable: {},

    //加载资源
    LoadRes: function(path, type, callback) {
        if (path in ResMgr.resPool) {
            return false;
        }
        cc.loader.loadRes(path, type, function (err, prefab) {
            //node = cc.instantiate(prefab);
            if (err==null) {
                if (path in ResMgr.resPool) {
                    cc.log("loadRes: "+path+" err:already exist");
                    if (callback!=null) {
                        callback(err,path);
                    }
                    return;
                }
                let newInfo = new ResInfo();
                newInfo.path = path;
                newInfo.obj = prefab;
                newInfo.type = type;
                //记录所有相关资源
                for (let key of cc.loader.getDependsRecursively(prefab)) {
                    newInfo.uuids[key] = true;
                    if (key in ResMgr.uuidTable) {
                        ResMgr.uuidTable[key] += 1;
                    }
                    else{
                        ResMgr.uuidTable[key] = 1;
                    }
                    //cc.log(path+"(getDependsRecursively):"+key);
                }
                ResMgr.resPool[path] = newInfo;
            }
            cc.log("loadRes: "+path+" err:"+err);
            if (callback!=null) {
                callback(err,path);
            }
        });
        return true;
    },
    //获取实例
    GetInstance: function(path) {
        let p = this.resPool[path];
        if(p!=null){
            p.ref++;
            if (p.type==cc.Prefab) {
                return cc.instantiate(p.obj);
            }
            else{
                return p.obj;
            }
        }
        return null;
    },
    GetInstanceArrary: function(path, num) {
        let arr = new Array();
        let p = this.resPool[path];
        if(p!=null && num>0){
            for (let index = 0; index < num; index++) {
                if (p.type==cc.Prefab) {
                    arr.push(cc.instantiate(p.obj));
                }
                else{
                    arr.push(p.obj);
                }
            }
            p.ref+=num;
        }
        return arr;
    },
    //释放实例
    ReleaseInstance: function(path, num=1) {
        let p = this.resPool[path];
        if(p!=null){
            p.ref -= num;
            //释放资源
            if (p.ref<=0) {
                this._ReleaseRes(p);
            }
        }
    },
    //释放资源
    _ReleaseRes: function(p) {
        //cc.loader.release(Object.keys(p.uuids));
        Object.keys(p.uuids).forEach(function(key){
            //cc.log(key+": "+LoadRes.uuidTable[key]);
            ResMgr.uuidTable[key]--;
            if (ResMgr.uuidTable[key] > 0) {
                delete p.uuids[key];
            }
            else{
                delete ResMgr.uuidTable[key];
            }
        });
        cc.loader.release(Object.keys(p.uuids));
        cc.loader.releaseRes(p.path);
        delete this.resPool[p.path];
    }
};
